<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="./react.js"></script>
<script src="./react-dom.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
    class App extends React.Component{
        state={
            num:0,
            msg:""
        }
        render(){
            return(
                <div>
                        <h1>我是一个父组件---{this.state.num}----{this.state.msg}</h1>    
                        {
                            // 父组件中的子组件标签上绑定一个自定义属性，自定义属性值必须为函数；
                        }
                        <Child getSum={(num)=>{
                            console.log(num)
                            this.setState({
                                num:num
                            })
                        }} getMsg={(msg)=>{
                            this.setState({
                                msg:msg
                            })
                        }}></Child>
                </div>
            )
        }
    }

    class Child extends React.Component{
        state={
            num:0
        }
        render(){
            return (
                <div>
                    <h2>我是一个子组件---{this.state.num}</h2>    
                    <button onClick={()=>{
                        this.setState((state)=>{
                            return {
                                num:state.num+1
                            }
                        },()=>{
                            // 通过this.props.属性名调用父组件传递的函数向父组件传递参数
                            console.log(this.props)
                            this.props.getSum(this.state.num)
                        })
                    }}>++</button>
                    <button onClick={()=>{
                        this.props.getMsg("好好学习天天向上")
                    }}>向父组件传值</button>
                </div>
            )
        }
    }

    ReactDOM.render(<App/>,document.getElementById("root"));
</script>
</html>